<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsSn">
        <el-input
          v-model="queryParams.goodsSn"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入收货人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option :label="item.dictLabel" :value="item.dictValue" v-for="(item, index) in orderStatusLists" :key="index"/>
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入门店"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="createTime">
        <el-date-picker
          v-model="timeRange"
          :value-format="formatDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收货人手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入收货人手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orderInfo:orderInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orderInfo:orderInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orderInfo:orderInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orderInfo:orderInfo:export']"
        >导出订单详情</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExportSeparate"
          v-hasPermi="['orderInfo:orderInfo:export']"
        >导出订单支付信息</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="orderInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <el-table-column label="支付相关单号" align="center" prop="parentOrderSn" />

      <el-table-column label="收货人" align="center" prop="consignee" />
      <el-table-column label="订单状态" align="center" prop="orderStatusName" />
      <el-table-column label="订单金额" align="center" prop="totalFee" />
      <el-table-column label="门店名称" align="center" prop="supplierName" />
      <el-table-column label="下单时间" align="center" prop="createTime" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="toDetail(scope.row)"
            v-hasPermi="['orderInfo:orderInfo:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleOpenGoodsInfo(scope.row)"
          >商品信息</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orderInfo:orderInfo:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="商品信息" :visible.sync="openGoodsInfo" width="1000px" append-to-body>
      <!-- <draggable-table class="hn-appointment-add-table" :loading="false" :hasOperationBtn="false" :data="goodsBaseInfo"
        :header="goodsBaseInfoTableHeader" :option="tableOption">
      </draggable-table> -->
      <el-table :data="goodsBaseInfo" :option="tableOption" >
        <el-table-column label="图片" align="center">
          <template slot-scope="scope">
            <div class="my-goods-img">
              <img :src="scope.row.goodsImg"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="goodsName" align="center"/>
        <el-table-column label="属性" prop="goodsAttr" align="center"/>
        <el-table-column label="商品供应商" prop="goodsSupplierName" align="center"/>
        <el-table-column label="结算价" prop="cost" align="center"/>
        <el-table-column label="第三方ID" prop="第三方ID" align="center"/>
        <el-table-column label="售价" prop="price" align="center"/>
        <el-table-column label="数量" prop="goodsNumber" align="center"/>
        <el-table-column label="小计" prop="goodsTotalPrice" align="center"/>
        <el-table-column label="操作" prop="" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="openPreview(scope.row)">预览</el-button>
          </template>
        </el-table-column>
      </el-table>
      
    </el-dialog>

    <el-drawer title="预览" :visible.sync="preview.open" direction="rtl" size="375px">
      <div class="preview-box">
        <div class="g-pic">
          <el-carousel height="375px">
            <el-carousel-item v-for="item in preview.swiperImage" :key="item.id">
              <img :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="g-info">
          <div class="g-price"><span>¥</span>{{preview.detail.price}}</div>
          <div class="g-name">{{preview.detail.goodsName}}</div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
        <div class="g-desc">
          <div class="g-desc-img" v-for="item in preview.detailImage" :key="item.id">
            <img v-if="item.fileType!='mp4'" :src="item.fileFullUrl+'?x-oss-process=image/resize,p_80'" />
            <video v-if="item.fileType=='mp4'" :src="item.fileFullUrl"></video>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { getGoods } from "@/api/goods/goods";
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo, exportOrderInfo } from "@/api/orderInfo/orderInfo";
import { getFileList} from "@/api/basic/file"
import * as orderInfoService from "@/api/orderInfo/orderInfo"
import draggableTable from '@/components/DraggableTable'
export default {
  name: "OrderInfo",
  components: {
      draggableTable
    },
  data() {
    return {
      orderStatusLists: [],
      formatDate: 'yyyy-MM-dd',
      timeRange: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单详情表格数据
      orderInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 是否分单字典
      isSeparateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSn: undefined, // 订单编号
        orderStatus: undefined, // 订单状态
        mobile: undefined, // 收货人手机号
        goodsSn: undefined, // 商品编号
        consignee: undefined, // 收货人
        supplierName: undefined, // 门店
        nickName: undefined, // 用户昵称
        createTimeStart: undefined, // 下单时间
        createTimeEnd: undefined
      },
      openGoodsInfo:false,
      tableOption: {
        border: true,
        minHeight: 400,
        hideInput: true,
        tableHeader: 'lightblue',
        tableClassName: 'hn-form-custom-table'
      },
      goodsBaseInfo: [],
      goodsBaseInfoTableHeader: [{
          prop: 'goodsImg',
          label: '图片',
          width: '120',
          render: (h, params) => {
            const product = params.row
            console.log('product:')
            console.log(product)
            console.log(product.goodsImg)
            if (product.goodsImg) {
              return h('div', {
                attrs: {
                  class: 'my-goods-img'
                },
                domProps: {
                  innerHTML: ('<img src="' + product.goodsImg + '"/>')
                }
              })
            } else {
              return '<span > </span>'
            }
          }
        }, {
          prop: 'goodsName',
          label: '名称'
        }, {
          prop: 'goodsAttr',
          label: '属性'
        }, {
          prop: 'goodsSupplierName',
          label: '商品供应商'
        }, {
          prop: 'cost',
          label: '结算价'
        }, {
          prop: '',
          label: '第三方ID'
        }, {
          prop: 'price',
          label: '售价'
        }, {
          prop: 'goodsNumber',
          label: '数量'
        }, {
          prop: 'goodsTotalPrice',
          label: '小计'
        }, {
          prop: '',
          label: '操作',
          render: (h, params) => {
            const row = params.row
            return  '<el-button size="mini" type="text" icon="el-icon-view" @click="openPreview('+row+')">查看</el-button>'
          }
        }],
        preview:{
          open:false,
          swiperImage:[],
          detailImage:[],
          detail:{}
        }
    };
  },
  created() {
    this.getList();
    this.orderStatusList();
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isSeparateOptions = response.data;
    });
  },
  methods: {
    openPreview(row){
      this.preview.open = true
      this.preview.swiperImage = []
      this.preview.detailImage = []
      this.preview.detail = row.baseInfo
      getFileList({
        sourcessId: row.baseInfo.id,
        sourcessType: 'GOODS_INFO',
        sourcessType2: 'SWIPER'
      }).then(res => {
        this.preview.swiperImage= res.rows
      })
      getFileList({
        sourcessId: row.baseInfo.id,
        sourcessType: 'GOODS_INFO',
        sourcessType2: 'DESCRIBE'
      }).then(res => {
        this.preview.detailImage= res.rows
      })
    },
    orderStatusList() {
      this.$webGlobal.startLoading()
      orderInfoService.orderStatusList().then((res) => {
        if (res && res.code && res.code === 200) {
          if (res.data && res.data.length) {
            this.orderStatusLists = res.data
          }
        } else {
          let msg = '获取订单状态枚举失败！'
          if (res.msg) {
            msg = res.msg
          }
          this.$message.error(msg)
        }
        this.$webGlobal.endLoading()
      }).catch((res) => {
        this.$webGlobal.endLoading()
        this.$message.error('获取订单状态枚举失败！')
      })
    },
    /** 详情按钮操作 */
    toDetail(row) {
      console.log(row)
      this.$router.push({
        name: 'orderInfoDetail',
        params: {
          id: row.id
        }
      })
    },
    /** 查询订单详情列表 */
    getList() {
      this.loading = true;
      if (this.timeRange) {
        console.log(this.timeRange)
        this.queryParams.createTimeStart = this.timeRange[0]
        this.queryParams.createTimeEnd = this.timeRange[1]
      }
      listOrderInfo(this.queryParams).then(response => {
        this.orderInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 是否分单字典翻译
    isSeparateFormat(row, column) {
      return this.selectDictLabel(this.isSeparateOptions, row.isSeparate);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.timeRange = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加订单详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getOrderInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单详情编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrderInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrderInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleExportSeparate() {
      let queryParams = JSON.parse(JSON.stringify(this.queryParams))
      queryParams.isSeparate=1
      this.$confirm('是否确认导出所有订单支付信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrderInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    handleOpenGoodsInfo(row){
      this.openGoodsInfo = true
      this.goodsBaseInfo = []
      this.getGoodsDetail(row.id)
    },
    getGoodsDetail(id) {
      this.$webGlobal.startLoading()
      orderInfoService.getGoodsInfo(id).then((res) => {
        if (res && res.code && res.code === 200) {
          if (res.rows && res.rows.length) {
            res.rows[0].goodsSupplierName = ''
            this.goodsBaseInfo = res.rows
            getGoods(this.goodsBaseInfo[0].goodsId).then(cres => {
              this.goodsBaseInfo[0].baseInfo = cres.data
              this.goodsBaseInfo[0].goodsSupplierName = cres.data.goodsSupplierName;
            });
            
          }
        } else {
          let msg = '获取商品信息失败！'
          if (res.msg) {
            msg = res.msg
          }
          this.$message.error(msg)
        }
        this.$webGlobal.endLoading()
      }).catch((res) => {
        this.$webGlobal.endLoading()
        this.$message.error('获取商品信息失败！')
      })
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
  .orderInfoDetail {
    font-size: 14px;
    padding: 20px;
    padding-top: 0;

    .editButtonGroup {
      margin: 0px 0 20px 0;
      text-align: right;
    }

    .el-steps--horizontal {
      margin-bottom: 20px;
    }
  }

  .my-goods-img {
    width: 100%;
  }

  .my-goods-img img {
    width: 100%;
  }


  .el-icon-remove{
    font-size: 36px;
    color: #F56C6C;
  }
  .el-icon-circle-plus{
    font-size: 36px;
    color: #409EFF
  }
  .g-pic img{
    width: 100%;
    height: auto;
    vertical-align: top;
  }
  .g-desc img{
    width: 100%;
    height: auto;
    vertical-align: top;
  }
  .g-info{
    padding: 18px 16px;
    background-color: #FFFFFF;
    margin-bottom: 6px;
  }
  .g-price{
    font-size: 22px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #FF5263;
  }
  .g-price span{
    font-size: 12px;
  }
  .g-name{
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: 500;
    color: #000000;
  }
  .el-drawer__body{
    background-color: #F3F5F7;
    height: 100%;
    overflow: auto;
  }
</style>
